package com.wnhoo.smartfishtank.api.data;


import com.wnhoo.smartfishtank.execption.FormCloudDataException;
import com.wnhoo.smartfishtank.util.Function;
import com.wnhoo.smartfishtank.util.MyLog;

/**
 * Created by kevinwen on 2014/11/16.
 * Copyright (c) 2014/11/16.KevinWen. All rights reserved.
 */
public class LM_DataTiming {

    private static final String TAG = "LM_DataTiming";
    private String time_open_enable ;//定时开的使能
    private String time_colse_enable ;;//定时关的使能
    private String time_open_value ;//开定时的时间,如12:50
    private String time_colse_value ;
    private String perion_value;//二进制字符，重复周期的值：一 ~ 六日，开启为1，否则为0
    private String perion_value_hex_open;//十六进制字符，定时开的使能开关+重复周期的值
    private String perion_value_hex_colse;//十六进制字符，定时关的使能开关+重复周期的值


    public static void main(){

        try {
            LM_DataTiming data = LM_DataTiming.formCloudData("01,1,12:30,FF","01,0,15:46,FF");
        }catch (FormCloudDataException e){
            MyLog.e(TAG, e.toString());
        }
    }

    /**
     * 从云端存储数据格式(01,1,12:30,FF;01,0,15:46,FF)转化为对象
     * 每次必须传入 定时开 和 定时关 两组数据
     * @param cloud_data_open
     * @param cloud_data_colse
     * @return
     */
    public static LM_DataTiming formCloudData(String cloud_data_open,String cloud_data_colse)throws FormCloudDataException{

        //01,1,12:30,FF;01,0,15:46,FF

        if(cloud_data_open.length()!=13||cloud_data_colse.length()!=13){

            throw new FormCloudDataException("数据格式不对");//抛出异常
        }

        String[] cloud_data_open_array = cloud_data_open.split(",");//01,1,12:30,FF
        String[] cloud_data_colse_array = cloud_data_colse.split(",");

        String open_value = cloud_data_open_array[2];//12:30
        String colse_value = cloud_data_colse_array[2];//15:46

        String open_hex = cloud_data_open_array[3];
        String colse_hex = cloud_data_colse_array[3];

        String open_int2 = Function.hexStringToInt2(open_hex);
        String colse_int2 = Function.hexStringToInt2(colse_hex);

        //MyLog.e(TAG, "week_temp_open="+week_temp_open);
        //MyLog.e(TAG, "week_temp_colse="+week_temp_colse);

        //定时开的使能
        String open_enable = open_int2.substring(0,1);
        //定时关的使能
        String colse_enable = colse_int2.substring(0,1);

        String perion_value_int2 = open_int2.substring(1, 8);

        LM_DataTiming dataTiming = new LM_DataTiming();
        dataTiming.setPerion_value(perion_value_int2);
        dataTiming.setPerion_value_hex_open(open_hex);
        dataTiming.setPerion_value_hex_colse(colse_hex);
        dataTiming.setTime_open_enable(open_enable);
        dataTiming.setTime_colse_enable(colse_enable);
        dataTiming.setTime_open_value(open_value);
        dataTiming.setTime_colse_value(colse_value);

        return dataTiming;
    }


    /**
     * 暂时没有用
     * @param enable
     * @return
     */
    public String getPerionHEXValue(boolean enable){
        String result_int2 = null;
        if(enable){
            result_int2 = "1" + getPerion_value();
        }else {
            result_int2 = "0" + getPerion_value();
        }
        return Function.String2ToHexString(result_int2);
    }

    /**
     * DataTiming实例转为协议格式数据【01,1,15:46,FF;01,0,15:47,FF】
     * @return 两组定时数组（定时开/定时关）
     */
    public String getCloudData() {
        //01,1,15:46,FF;01,0,15:47,FF
        StringBuffer sb = new StringBuffer();
        sb.append("01,1,").append(getTime_open_value()).append(",").append(getPerion_value_hex_open()).append(";")
                .append("01,0,").append(getTime_colse_value()).append(",").append(getPerion_value_hex_colse());
        return sb.toString();
    }

    public String getTime_open_enable() {
        return time_open_enable;
    }

    public void setTime_open_enable(String time_open_enable) {
        this.time_open_enable = time_open_enable;
    }

    public String getTime_colse_enable() {
        return time_colse_enable;
    }

    public void setTime_colse_enable(String time_colse_enable) {
        this.time_colse_enable = time_colse_enable;
    }

    public String getTime_open_value() {
        return time_open_value;
    }

    public void setTime_open_value(String time_open_value) {
        this.time_open_value = time_open_value;
    }

    public String getTime_colse_value() {
        return time_colse_value;
    }

    public void setTime_colse_value(String time_colse_value) {
        this.time_colse_value = time_colse_value;
    }

    public String getPerion_value() {
        return perion_value;
    }

    public void setPerion_value(String perion_value) {
        this.perion_value = perion_value;
    }

    public String getPerion_value_hex_open() {
        if(perion_value_hex_open==null){

            String result_int2 = getTime_open_enable() + getPerion_value();
            perion_value_hex_open = Function.String2ToHexString(result_int2);
        }
        return perion_value_hex_open;
    }

    public void setPerion_value_hex_open(String perion_value_hex_open) {
        this.perion_value_hex_open = perion_value_hex_open;
    }

    public String getPerion_value_hex_colse() {

        if(perion_value_hex_colse==null){

            String result_int2 = getTime_colse_enable() + getPerion_value();
            perion_value_hex_colse = Function.String2ToHexString(result_int2);
        }
        return perion_value_hex_colse;
    }

    public void setPerion_value_hex_colse(String perion_value_hex_colse) {

        this.perion_value_hex_colse = perion_value_hex_colse;
    }

    @Override
    public String toString() {
        return "LM_DataTiming{" +
                "time_open_enable='" + time_open_enable + '\'' +
                ", time_colse_enable='" + time_colse_enable + '\'' +
                ", time_open_value='" + time_open_value + '\'' +
                ", time_colse_value='" + time_colse_value + '\'' +
                ", perion_value='" + perion_value + '\'' +
                ", perion_value_hex_open='" + perion_value_hex_open + '\'' +
                ", perion_value_hex_colse='" + perion_value_hex_colse + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LM_DataTiming that = (LM_DataTiming) o;

        if (!perion_value.equals(that.perion_value)) return false;
        if (!perion_value_hex_colse.equals(that.perion_value_hex_colse)) return false;
        if (!perion_value_hex_open.equals(that.perion_value_hex_open)) return false;
        if (!time_colse_enable.equals(that.time_colse_enable)) return false;
        if (!time_colse_value.equals(that.time_colse_value)) return false;
        if (!time_open_enable.equals(that.time_open_enable)) return false;
        if (!time_open_value.equals(that.time_open_value)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = time_open_enable.hashCode();
        result = 31 * result + time_colse_enable.hashCode();
        result = 31 * result + time_open_value.hashCode();
        result = 31 * result + time_colse_value.hashCode();
        result = 31 * result + perion_value.hashCode();
        result = 31 * result + perion_value_hex_open.hashCode();
        result = 31 * result + perion_value_hex_colse.hashCode();
        return result;
    }
}
